package com.clerkq.CircleList
{
	
	import com.clerkq.CircleList.data.ThumbHVO;
	import com.clerkq.CircleList.data.ThumbVVO;
	import com.clerkq.CircleList.event.CircleListEvent;
	import com.clerkq.CircleList.view.ThumbH;
	import com.clerkq.CircleList.view.ThumbListH;
	import com.content.model.Collection.ItemGroup;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	import gs.TweenLite;
	
	public class CircleListH extends UIObject implements IDisposable
	{
		private var _bg:Shape;
		private var _contentMask:Shape;
		private var _thumbList:ThumbListH;
		
		private var _visibleNum:int;
		private var _width:int;
		private var _height:int;
		private var _thumbWidth:int;
		private var _thumbHeight:int;
		private var _thumbSpace:int;
		private var _margin:Rectangle;
		private var _isMovement:Boolean;	// If the total thumbs number less than display number, isMovement should be False
		
		private var _selectedThumb:ThumbH;
		private var _itemList:ItemGroup;
		
		public function CircleListH(visibleNum:int, thumbSpace:int = 130, thumbWidth:int = 120, thumbHeight:int = 150, margin:Rectangle = null)
		{
			super();
			_thumbSpace = thumbSpace;
			_thumbWidth = thumbWidth;
			_thumbHeight = thumbHeight;
			_margin = margin;
			
			_visibleNum = visibleNum;
			_width = _thumbSpace * visibleNum - (_thumbSpace - _thumbWidth) + _margin.left + _margin.right;
			_height =  _thumbHeight + _margin.top + _margin.bottom;
			
			_bg = new Shape();//Geometry.drawRect(1, 1, 0xaaffcc, 0.01, -1);
			addChild(_bg);
			_bg.graphics.beginFill(0xaaffcc, 0.01);
			_bg.graphics.drawRect(0,0,_width, _height);
			
			_thumbList = new ThumbListH(visibleNum, _thumbSpace, _thumbWidth);
			addChild(_thumbList);
			
			_contentMask = new Shape();//Geometry.drawRect(1, _height, 0xffff00, 0.6);
			addChild(_contentMask);
			_contentMask.graphics.beginFill(0xffff00, 0.6);
			_contentMask.graphics.drawRect(0,0,5, 5);
			_thumbList.mask = _contentMask;
			
			
			_bg.width = _width;
			_bg.height = _height;
			_contentMask.width = _thumbSpace * visibleNum - (_thumbSpace - _thumbWidth);
			_contentMask.height = _thumbHeight;
			
			_bg.x  = 0;
			_bg.y  = 0;
			
			_thumbList.x = _margin.left
			_thumbList.y = int(_thumbHeight/2) + _margin.top;
			
			_contentMask.x = _margin.left;
			_contentMask.y = _margin.top;
			
//			_thumbList.addEventListener(CircleListEvent.UPDATE_COMPLETE,onUpdateListComplete);
			this.addEventListener(MouseEvent.CLICK, OnClick);
		}
		
		// Move active thumb to previous ///////////////////
		public function GoPrevious():ThumbH{
			// Get the previous index
			var newSeq:int
			var activeSeq:int = IndexToSeq(_selectedThumb.index);
			var len:int = _itemList.Length;
			if(activeSeq <= 0){
				newSeq = len-1;
			}else{
				newSeq = activeSeq - 1;
			}
			SelectThumbBySeq(newSeq);
			var activeThumb:ThumbH = _thumbList.thumbPipe[newSeq] as ThumbH;
			
			var moveStep:int = 0;
			var dest:Number = 0;	
			if(_isMovement){
				if(newSeq < _thumbList.leftBoundSeq ){
					dest = (_thumbList.thumbPipe[newSeq].x - _thumbWidth/2) * (-1);
				}
				else if(newSeq > _thumbList.rightBoundSeq){
					moveStep = (len - newSeq) + _thumbList.leftBoundSeq;
					dest = moveStep * _thumbSpace - (_thumbList.thumbPipe[_thumbList.leftBoundSeq].x - _thumbWidth/2);
				}
				Move(dest, true);
								
			}					 
			//return newSeq;
			return activeThumb;
		}
		
		// Move active thumb to next
		public function GoNext():ThumbH{
			// Get the next index
			var newSeq:int;
			var activeSeq:int = IndexToSeq(_selectedThumb.index);
			var len:int = _itemList.Length;
			if(activeSeq >= len-1){
				newSeq = 0;
			}else{
				newSeq = activeSeq + 1;
			}			
			SelectThumbBySeq(newSeq);
			var activeThumb:ThumbH = _thumbList.thumbPipe[newSeq] as ThumbH;
			
			var moveStep:int = 0;
			var dest:Number = 0;
			if(_isMovement){
				if(newSeq > _thumbList.rightBoundSeq ){
					dest = _thumbList.right - (_thumbList.thumbPipe[newSeq].x + _thumbWidth/2);
				}
				else if(newSeq < _thumbList.leftBoundSeq){
					moveStep = newSeq + (len - _thumbList.rightBoundSeq);
					dest = -moveStep * _thumbSpace + (_thumbList.right - (_thumbList.thumbPipe[_thumbList.rightBoundSeq].x + _thumbWidth/2));
				}
				Move(dest, true);
			}	
			//return newSeq;
			return activeThumb;
		}
		
		// use nearest direction
		public function Goto(index:int):ThumbH{
			
			var newSeq:int = IndexToSeq(index);
			SelectThumbBySeq(newSeq);
			var activeThumb:ThumbH = _thumbList.thumbPipe[newSeq] as ThumbH;
			
			// Move or Not
			if(newSeq > _thumbList.rightBoundSeq || newSeq < _thumbList.leftBoundSeq){
				
				var leftSpace:int = Math.abs(newSeq - _thumbList.leftBoundSeq);
				var rightSpace:int = Math.abs(newSeq - _thumbList.rightBoundSeq);
				
				var moveStep:int;
				var dest:Number = 0;
				if(leftSpace < rightSpace){
					//direct = "left";
					dest = (_thumbList.thumbPipe[newSeq].x - _thumbWidth/2) * (-1);
				}else{
					//direct = "right";
					dest = _thumbList.right - (_thumbList.thumbPipe[newSeq].x - _thumbWidth/2 + _thumbWidth);
				}
				// Goto
				Move(dest, true);
			}
			return activeThumb;
		}
		
		public function Move(distance:Number, isAnimate:Boolean = false):void{
			
			// Update the list and move it
            if(_isMovement){
            	_thumbList.MoveList(distance);
            	if(isAnimate){
            		if(distance != 0){            			
//						Tweener.removeTweens(_thumbList);
//						Tweener.addTween(_thumbList, {y:(PAGE_SPAN), time:0.5, delay:0, transition:"easeOutExpo"});
						TweenLite.to(_thumbList, 0.5, {x:_margin.left});
            		}		
				}else{
					_thumbList.x += distance;
				}
			}
		}
		
		// "Seq" is item index in Array, "index" is one of the property for item
		private function IndexToSeq(index:int):int{
			var i:int;
			var len:uint = _itemList.Length;
			for( i=0; i<len; i++ ){
				if(index == ThumbH(_thumbList.thumbPipe[i]).index){
					return i;
				}
			}
			return -1;	
		}
		
		private function SeqToIndex(seq:int):int{
			return ThumbH(_itemList.Items[seq]).index;
		}
		
		private function SelectThumbBySeq(seq:int):ThumbH{
			if(_selectedThumb != null){
				_selectedThumb.isActive = false;
			}
			var curThumb:ThumbH = _thumbList.thumbPipe[seq] as ThumbH;
			curThumb.isActive = true;
			return _selectedThumb = curThumb;
		}
		
		/* Events Handler
		 * ------------------------------------------------------ */
//		private function onUpdateListComplete(evt:CircleListEvent):void{}
		
		private function OnClick(evt:MouseEvent):void{
			if (evt.target is ThumbH){
				if(_selectedThumb != null){
					_selectedThumb.isActive = false;
				}
				var thumb:ThumbH = evt.target as ThumbH;
				thumb.isActive = true;
				_selectedThumb = thumb;
			}
		}
		
		public function get curVO():ThumbHVO{	return _selectedThumb.thumbVO;	}
		override public function get width():Number{	return _bg.width;		}
		
		public function FillList(itemList:ItemGroup):void{
			_itemList = itemList;
			_isMovement = (_visibleNum < itemList.Length)?true:false;
			_thumbList.FillList(itemList);
			
			// 默认选中第一个，并发出一个模拟事件
			var curThumb:ThumbH = SelectThumbBySeq(IndexToSeq(0));
//			curThumb.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true));
			
		}
		
		public function Dispose():void
		{
			if(_thumbList != null){				
//				_thumbList.removeEventListener(CircleListEvent.UPDATE_COMPLETE,onUpdateListComplete);
				this.removeEventListener(MouseEvent.CLICK, OnClick);
				_thumbList.Dispose();
			}
		}
		
	}
}